using System.Collections.Generic;
using System.Linq;
using DumpsterDoggy.Domain.Address;
using DumpsterDoggy.Domain.Shipping.Calculators;

namespace DumpsterDoggy.Domain.Orders
{
    public class Order : IOrder
    {
        private readonly List<IProduct> products = new List<IProduct>();
        private readonly IList<IShippingCalculator> shippingCalculators;

        public Order(IList<IShippingCalculator> shippingCalculators)
        {
            this.shippingCalculators = shippingCalculators;
        }

        #region IOrder Members

        public IAddress Address { get; set; }

        public decimal CalculateLowestShippingFee()
        {
            return shippingCalculators.Where(c => c.IsValidFor(this)).Min(c => c.CalculateCharge(this));
        }

        public decimal CalculateHighestShippingFee()
        {
            return shippingCalculators.Where(c => c.IsValidFor(this)).Max(c => c.CalculateCharge(this));
        }

        public IEnumerable<IProduct> Products
        {
            get { return products; }
        }

        public void AddProduct(IProduct product)
        {
            products.Add(product);
        }

        public decimal SubTotal()
        {
            return products.Sum(p => p.Cost*p.Quantity);
        }

        public bool ContainsHazardousMaterials()
        {
            return products.Any(p => p.IsHazardous);
        }

        public bool RequiresForkLift()
        {
            return products.Any(p => p.RequiresForkList);
        }

        #endregion
    }
}